package com.example.android427;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MyBaseAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<String> list;
    private Map<String, Boolean> selectedMap = new HashMap<>();
    private ViewHolder holder = null;
    private SetOnClickDialogListener mSetOnClickDialogListener;


    public void OnSetOnClickDialogListener(SetOnClickDialogListener listener) {
        this.mSetOnClickDialogListener = listener;
    }
    public interface SetOnClickDialogListener{
        void onClickDialogListener(int type, boolean boolClick);
    }

    public MyBaseAdapter(Context mContext, ArrayList<String> mList, Map<String, Boolean> mSelectedMap){
        this.context = mContext;
        this.list = mList;
        this.selectedMap = mSelectedMap;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.shezhi_item,parent,false);
            holder = new ViewHolder();
            holder.switch_item =  (Switch)convertView.findViewById(R.id.switch_item);
            holder.tv_item =   (TextView) convertView.findViewById(R.id.tv_item);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_item.setText(list.get(position));
        holder.switch_item.setChecked(selectedMap.get(list.get(position)));
        holder.switch_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(!buttonView.isPressed()){
                    return;
                }
                if(mSetOnClickDialogListener != null){
                    mSetOnClickDialogListener.onClickDialogListener(position, isChecked);
                }
            }
        });
        return convertView;
    }

    private class ViewHolder{
        Switch switch_item;
        TextView tv_item;
    }

}
